
#快速查找 注册了100w条path,匹配一条path的时间在0s左右
class fast_find:
    def __init__(self,bind_func=None):
        self.bind_func=bind_func
        self.fast_dict={}#快速搜索字典
    #添加路径
    def add(self,**kwargs):
        """
        path:路径 如/eee/eer/#
        info:{"path":path,"handle":func} func是执行的回调函数
        """
        path=kwargs.get("path")
        if self.bind_func is not None:
            self.bind_func(path)
        new_dct=self.fast_dict
        for i in path:
            new_dct2=new_dct.get(i)
            if new_dct2 is None:
                new_dct[i]={}
                new_dct=new_dct[i]
            else:
                new_dct=new_dct2
        new_dct['value']=kwargs 
        # print("绑定path:",path)
    #查找路径
    def find(self,path):
        match_path=''
        now_dict=self.fast_dict
        for i in path:
            # print('find',i,topics)
            if '#'in now_dict:
                return {"match_path":match_path+'#',"info":now_dict['#']['value']}
            else:
                match_path+=i
                now_dict=now_dict.get(i)
                if now_dict is None:return#没有匹配到
        if now_dict.get('value'):return {"match_path":match_path,"info":now_dict['value']}
        elif now_dict.get('#')is not None:return {"match_path":match_path+'#',"info":now_dict['#']['value']}
        else:return None#没有匹配到
    def handle(self,path,data):
        now_info=self.find(path)#查找topic
        #如果对topic进行处理
        if now_info is not None:
            # print("匹配到path:",path)
            # print("\t\t数据:",data)   
            #执行对应topic的handle函数
            now_info['info']['handle'](data=data,path=path)
fast_find_obj=fast_find()
fast_find_obj.add(path='/api/0/v0/login',handle=lambda path,data:print("匹配到/api/0/v0/login:",path,data))
fast_find_obj.add(path='/api/0/v0/html',handle=lambda path,data:print("匹配到/api/0/v0/html:",path,data))
print(fast_find_obj.fast_dict)
fast_find_obj.handle(path='/api/0/v0/login',data='1')
fast_find_obj.handle(path='/api/0/v0/html',data='1')
input("")
fast_find_obj.add(path='/eee/ttt/#',handle=lambda path,data:print("匹配到/eee/ttt/#:",path,data))
fast_find_obj.add(path='/eee/rrr/#',handle=lambda path,data:print("匹配到/eee/rrr/#:",path,data))
fast_find_obj.add(path='/eee/aaaa',handle=lambda path,data:print("匹配到/eee/aaaa:",path,data))
fast_find_obj.add(path='/eee/w/#',handle=lambda path,data:print("匹配到/eee/w/#:",path,data))

fast_find_obj.handle(path='/eee/ttt/123',data='1')
fast_find_obj.handle(path='/eee/rrr/#',data='2')
fast_find_obj.handle(path='/eee/rrr/2',data='3')
fast_find_obj.handle(path='/eee/aaaa/tt',data='4')
fast_find_obj.handle(path='/eee/aaaa',data='5')
fast_find_obj.handle(path='/eee/w',data='6')
fast_find_obj.handle(path='/eee/w/',data='7')
fast_find_obj.handle(path='/eee/w/ef/ew',data='8')
import random  
import string  ,time
  
def generate_random_string(length=20):  
    # 定义可能出现在随机字符串中的字符集  
    characters = string.ascii_letters + string.digits  
    # 生成一个指定长度的随机字符串  
    random_string = ''.join(random.choice(characters) for i in range(length))  
    return random_string  
a=0
for i in range(100_0000):
    if i%10000==0:print(f"添加path:{i}/1000000({'%.0f'%(i/10000)}%)")
    fast_find_obj.add(path=generate_random_string(),handle=lambda path,data:print("匹！？"))
last_generate=generate_random_string()
fast_find_obj.add(path=last_generate,handle=lambda path,data:print("匹配到啦:",path,data))
print("开始匹配:",last_generate)
t=time.time()
fast_find_obj.handle(path=last_generate,data='嘿嘿嘿')
print("匹配",last_generate,"耗时:",time.time()-t)

last_generate2=generate_random_string()
print("开始匹配:",last_generate2)
t=time.time()
fast_find_obj.handle(path=last_generate2,data='嘿嘿嘿2')
print("匹配",last_generate2,"耗时:",time.time()-t)